package cn.appsys.controller.backend;

import cn.appsys.pojo.BackendUser;
import cn.appsys.service.backend.BackendUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/manager")
public class UserLoginController {

    @Autowired
    private BackendUserService backendUserService;

    @RequestMapping("/login")
    public String login(){
        return "backendlogin";
    }

    @RequestMapping("/dologin")
    public String dologin(String userCode, String userPassword, Model model, HttpSession session){
        BackendUser user = backendUserService.getLoginUser(userCode);
        if (null==user){
            model.addAttribute("error","用户名不存在!");
        }else {
            if (user.getUserPassword().equals(userPassword)){
                session.setAttribute("userSession",user);
                return "redirect:/manager/backend/main";
            }else {
                model.addAttribute("error","密码输入错误！");
            }
        }
        return "backendlogin";
    }

    @RequestMapping("/backend/main")
    public String main(HttpSession session){
        if (session.getAttribute("userSession")==null){
            return "redirect:manager/login";
        }
        return "backend/main";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("userSession");
        return "backendlogin";
    }
}
